﻿using Model;

namespace Dal
{
    public class houseDal
    {
        private readonly MyDbContext db;

        public houseDal(MyDbContext db)
        {
            this.db = db;
        }

        /// <summary>
        /// 房型添加
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public int HouseTypeAdd(houseModel info)
        {
            try
            {
                db.houseModel.Add(info);
                return db.SaveChanges();
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 房型显示查询
        /// </summary>
        /// <param name="indexs"></param>
        /// <param name="counts"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public List<houseModel> HouseTypeList(out int indexs,out int counts,int pageindex=1,int pagesize=2)
        {
            try
            {
                List<houseModel> list = db.houseModel.Where(x=>x.ISDelete == false).ToList();
                counts = list.Count();
                indexs = (int)Math.Ceiling(counts * 1.0 / pagesize);
                list = list.OrderBy(x=>x.H_id).SkipLast((pageindex -1 )* pagesize).Take(pagesize).ToList();
                return list;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 房型信息是否重复
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public int HouseSearchName(string name)
        {
            try
            {
                var list = db.houseModel.Where(x=>x.H_name == name).Count();
                return list;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="h_id"></param>
        /// <returns></returns>
        public int DeleteInfo(int h_id)
        {
            try
            {
                var res = db.houseModel.Find(h_id);
                res.ISDelete = true;
                return db.SaveChanges();
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="h_state"></param>
        /// <param name="h_id"></param>
        /// <returns></returns>
        public int UplowState(int h_state,int h_id)
        {
            try
            {
                var res = db.houseModel.Find(h_id);
                res.H_state = h_state;
                return db.SaveChanges();
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}